<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use common\models\City;
use yii\helpers\ArrayHelper;
use backend\models\OperatorRelation;
use backend\models\Image;
use \backend\models\VrPos;
use yii\web\View;
 
/* @var $this yii\web\View */
/* @var $model backend\models\BuildCity */
/* @var $form yii\widgets\ActiveForm */
$this->registerJs("
  ",View::POS_END);
?>
<script type="text/javascript">
    <!--

    function deleteItem(itemId, posId, spaceId)
    {

        if(!del_sure()){return false;}

        $.ajax({
            url:"index.php?r=vr/delete-item&itemId="+itemId+"&posId="+posId,
            data:{},
            //dataType:"json",
            success:function(data){

                var ret = JSON.parse(data);
                if(ret.error === 0){
                    var url = location.href;
                    url = url.indexOf('#spaceAnchor') > 0 ? url.replace(/\#spaceAnchor(\d)/, '#spaceAnchor' + spaceId) : url + '#spaceAnchor' + spaceId;
                    location.href = url;
                    location.reload(true);
                }else{
                    alert(ret.msg);
                }
            },
            error: function(){
                //请求出错处理
                alert("Error!");
            }
        });
    }

    function del_sure(){
        var gnl=confirm("确定要删除?");
        if (gnl==true){
            return true;
        }else{
            return false;
        }
    }

    function addItem(vrId, spaceId){
        var itemId = $("input[name='addItemId_"+spaceId+"']").val();

        $.ajax({
            url:"index.php?r=vr/add-item&itemId="+itemId+"&vrId="+vrId + "&spaceId=" + spaceId,
            data:{},
            //dataType:"json",
            success:function(data){
                var ret = JSON.parse(data);
                if(ret.error === 0){
                    var url = location.href;
                    url = url.indexOf('#spaceAnchor') > 0 ? url.replace(/\#spaceAnchor(\d)/, '#spaceAnchor' + spaceId) : url + '#spaceAnchor' + spaceId;
                    location.href = url;
                    location.reload(true);
                }else{
                    alert(ret.msg);
                }
            },
            error: function(){
                //请求出错处理
                alert("Error!");
            }
        });
    }

    //-->
</script>
<div class="build-city-form">

 <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
    <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
    <?= $form->field($model, 'city')->textInput() ?>
    <?= $form->field($model, 'village')->textInput() ?>
    <?= $form->field($model, 'house_type')->textInput() ?>
    <?= $form->field($model, 'area')->textInput() ?>
	<?= $form->field($model, 'cost')->textInput() ?>
    <?= $form->field($model, 'brief')->textInput() ?>
    <?= $form->field($model, 'vr_flag')->radioList(\backend\models\Vr::$vr_flag) ?>
    <?= $form->field($model, 'app_return')->radioList(\backend\models\Vr::$app_return) ?>
    <?= $form->field($model, 'image_upload')->fileInput(['class' => 'btn', 'style' => 'background-color:#ccc']) ?>
    <div>
        <?php
        echo !empty($model->vr_img)?"<img width='10%' src='".Image::show_image($model->vr_img)."'>":'无图';
        ?>
    </div>

    <?= $form->field($model, 'desc')->widget('backend\widgets\ueditor\UEditor',[
        'options'=>[
            'initialFrameWidth' => 850,
        ]
    ]) ?>
    <!--编辑空间-->
    <div>
        <?php

        foreach ($allSpace as $v){

            echo "<div style='border:1px solid #ccc;margin:10px;'>";
            echo "<div class='btn btn-primary'><a name='space{$v['space_id']}'></a>".$v['name']."</div>";


            foreach (explode(',', $v['item_id_list']) as $itemId){
                if((int)$itemId > 0){
                        echo  "<img width='10%' src='".Image::show_image(VrPos::getItemInfo($itemId)->img_small_id)."'><input type='button' id='delete_build_type' onclick='deleteItem({$itemId},{$v['id']},{$v['space_id']})' value='删除".$itemId."'/>";
                }
            }

            echo Html::input('text', "addItemId_{$v['space_id']}",'',['class' => '','placeholder'=>'单品ID','style'=>"margin-left:10px;width: 100px;"]);
            echo "<input type='button' class='' style='cursor:pointer;' onclick='javascript:addItem($model->id,{$v['space_id']})' value='添加'></span>";

            echo "</div>";
        }

        ?>

    </div>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') :  Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>
    <?php ActiveForm::end(); ?>

</div>
